home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / fromdl / fromdll.txt < prev    next >
Text File  |  1995-01-19  |  7KB  |  241 lines

  1. To Anyone that wishes to load strings and bitmaps from a DLL into
  2. a Visual Basic application, here's the source, Merry Christmas...
  3.  
  4. Note: Is project is also being uploaded as FROMDLL.ZIP, along with
  5. the TEST.DLL, that was produced with MSVC++ 
  6.  
  7.  
  8. First, this is the definations from FROMDLL.BAS file
  9. ----------------------------------------------------
  10. Declare Function LoadLibrary Lib "Kernel" (ByVal lpLibFileName As String) As Integer
  11. Declare Sub FreeLibrary Lib "Kernel" (ByVal hLibModule As Integer)
  12.  
  13. Declare Function LoadString Lib "User" (ByVal hInstance As Integer, ByVal wID As Integer, ByVal lpBuffer As Any, ByVal nBufferMax As Integer) As Integer
  14.  
  15. Declare Function LoadBitmap Lib "User" (ByVal hInstance As Integer, ByVal lpBitmapName As Any) As Integer
  16. Declare Function CreateCompatibleDC Lib "GDI" (ByVal hDC As Integer) As Integer
  17. Declare Function SelectObject Lib "GDI" (ByVal hDC As Integer, ByVal hObject As Integer) As Integer
  18. Declare Function BitBlt Lib "GDI" (ByVal hDestDC As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hSrcDC As Integer, ByVal XSrc As Integer, ByVal YSrc As Integer, ByVal dwRop As Long) As Integer
  19. Declare Function DeleteDC Lib "GDI" (ByVal hDC As Integer) As Integer
  20. Declare Function DeleteObject Lib "GDI" (ByVal hObject As Integer) As Integer
  21.  
  22. ' In the test.dll
  23. Declare Function SayHi Lib "TEST.DLL" () As Integer
  24.  
  25. Global Const SRCCOPY = &HCC0020 ' (DWORD) dest = source
  26.  
  27. Global DLLID%  ' global handle to the DLL
  28.  
  29. ------------------------------------------------------------
  30. And THIS is the FROMDLL.FRM file, I included the actual form
  31. defination so you can see the controls on the form and their
  32. properties...
  33. ------------------------------------------------------------
  34. VERSION 2.00
  35. Begin Form Form1 
  36.    BackColor       =   &H00C0C0C0&
  37.    Caption         =   "From a DLL"
  38.    ClientHeight    =   5700
  39.    ClientLeft      =   1125
  40.    ClientTop       =   2715
  41.    ClientWidth     =   7245
  42.    Height          =   6105
  43.    Icon            =   FROMDLL.FRX:0000
  44.    Left            =   1065
  45.    LinkTopic       =   "Form1"
  46.    ScaleHeight     =   5700
  47.    ScaleWidth      =   7245
  48.    Top             =   2370
  49.    Width           =   7365
  50.    Begin PictureBox Picture2 
  51.       Height          =   2895
  52.       Left            =   3720
  53.       ScaleHeight     =   2865
  54.       ScaleWidth      =   3405
  55.       TabIndex        =   8
  56.       Top             =   2460
  57.       Width           =   3435
  58.    End
  59.    Begin CommandButton btnClearPic 
  60.       Caption         =   "Clear Picture"
  61.       Height          =   495
  62.       Left            =   1980
  63.       TabIndex        =   7
  64.       Top             =   1860
  65.       Width           =   1695
  66.    End
  67.    Begin CommandButton btnPic 
  68.       Caption         =   "Load &Picture"
  69.       Height          =   495
  70.       Left            =   180
  71.       TabIndex        =   6
  72.       Top             =   1860
  73.       Width           =   1695
  74.    End
  75.    Begin PictureBox Picture1 
  76.       AutoRedraw      =   -1  'True  THIS IS VERY IMPORTANT !!!!
  77.       Height          =   2895
  78.       Left            =   180
  79.       ScaleHeight     =   191
  80.       ScaleMode       =   3  'Pixel
  81.       ScaleWidth      =   227
  82.       TabIndex        =   5
  83.       Top             =   2460
  84.       Width           =   3435
  85.    End
  86.    Begin CommandButton btnCallDll 
  87.       Caption         =   "Call DLL"
  88.       Height          =   495
  89.       Left            =   180
  90.       TabIndex        =   4
  91.       Top             =   1200
  92.       Width           =   1695
  93.    End
  94.    Begin CommandButton btnStr2 
  95.       Caption         =   "String 2"
  96.       Height          =   495
  97.       Left            =   1980
  98.       TabIndex        =   3
  99.       Top             =   600
  100.       Width           =   1695
  101.    End
  102.    Begin CommandButton btnStr1 
  103.       Caption         =   "String 1"
  104.       Height          =   495
  105.       Left            =   180
  106.       TabIndex        =   2
  107.       Top             =   600
  108.       Width           =   1695
  109.    End
  110.    Begin TextBox Text1 
  111.       Height          =   285
  112.       Left            =   180
  113.       TabIndex        =   1
  114.       Top             =   180
  115.       Width           =   6975
  116.    End
  117.    Begin CommandButton btnExit 
  118.       Caption         =   "E&xit"
  119.       Height          =   495
  120.       Left            =   5880
  121.       TabIndex        =   0
  122.       Top             =   1200
  123.       Width           =   1215
  124.    End
  125.    Begin Label Label2 
  126.       AutoSize        =   -1  'True
  127.       BackStyle       =   0  'Transparent
  128.       Caption         =   "Copied from other Picture"
  129.       Height          =   195
  130.       Left            =   3720
  131.       TabIndex        =   10
  132.       Top             =   5400
  133.       Width           =   2175
  134.    End
  135.    Begin Label Label1 
  136.       AutoSize        =   -1  'True
  137.       BackStyle       =   0  'Transparent
  138.       Caption         =   "Loaded from DLL"
  139.       Height          =   195
  140.       Left            =   180
  141.       TabIndex        =   9
  142.       Top             =   5400
  143.       Width           =   1470
  144.    End
  145. End
  146.  
  147. Option Explicit
  148.  
  149. ' Make a SIMPLE call to the TEST.DLL
  150. Sub btnCallDll_Click ()
  151.  
  152. Dim r%
  153. r% = SayHi()
  154.  
  155. End Sub
  156.  
  157. Sub btnClearPic_Click ()
  158. picture2.Picture = LoadPicture()
  159. picture1.Picture = LoadPicture()
  160. End Sub
  161.  
  162. Sub btnExit_Click ()
  163. ' Make sure to UNLOAD the DLL from memory on exit
  164.  
  165. FreeLibrary (DLLID%)
  166. End
  167.  
  168. End Sub
  169.  
  170. Sub btnPic_Click ()
  171. ' The REAL MAGIC !!!
  172.  
  173. Dim hMyBitmap%, hdcmemory%
  174. Dim r%, dccontrol%, cx%, cy%, pict1%
  175.  
  176. picture1.ScaleMode = 3
  177.  
  178. dccontrol% = picture1.hDC
  179. cx% = picture1.ScaleWidth
  180. cy% = picture1.ScaleHeight
  181.  
  182. ' load bitmap from DLL
  183. hMyBitmap% = LoadBitmap(DLLID%, "CPARROW")
  184.  
  185. ' Create context compatible with the picture box
  186. hdcmemory% = CreateCompatibleDC(dccontrol%)
  187. ' select bitmap into device context
  188. r% = SelectObject(hdcmemory%, hMyBitmap%)
  189.  
  190. ' Copy it
  191. r% = BitBlt(dccontrol%, 0, 0, cx%, cy%, hdcmemory%, 0, 0, SRCCOPY)
  192.  
  193. picture1.Picture = picture1.Image ' move presistent image to visible
  194. picture2.Picture = picture1.Picture ' prove it can be moved
  195.  
  196. r% = DeleteDC(hdcmemory%)      ' delete device context
  197. r% = DeleteObject(hMyBitmap%)  ' delete object (free memory!)
  198.  
  199. End Sub
  200.  
  201. Sub btnStr1_Click ()
  202.  
  203. Dim temp$, r%
  204.  
  205. temp$ = Space(80) ' allocate space before calling API
  206. r% = LoadString(DLLID%, 1, temp$, Len(temp$))  ' the '1' is the string id
  207. text1 = Trim(temp$)
  208.  
  209. End Sub
  210.  
  211. Sub btnStr2_Click ()
  212.  
  213. Dim temp$, r%
  214.  
  215. temp$ = Space(80) ' allocate space before calling API
  216. r% = LoadString(DLLID%, 2, temp$, Len(temp$)) ' the '2' is the string id
  217. text1 = Trim(temp$)
  218.  
  219. End Sub
  220.  
  221. Sub Form_Load ()
  222. ' Load the DLL into memory and get handle to it
  223.  
  224. DLLID% = LoadLibrary("TEST.DLL")
  225. If DLLID% < 32 Then
  226.     MsgBox "Error loading DLL"
  227.     End
  228. End If
  229.  
  230. End Sub
  231.  
  232.  
  233. -----------------------------------------------------
  234. That's all there is to it...  Hope this help's you
  235. out.
  236.  
  237. Have fun,
  238.         Edward Lyk
  239.         internet:   EDLYK@delphi.com
  240.  
  241.